<?php
/**
 * Menu_IndexController
 *  
 * @author Chashin Evgeniy
 * @version 1.0.1
 */
class Menu_IndexController extends Zend_Controller_Action {
	
	public function indexAction() {
		$model = new Menu_Model_Tree ();
		$menu_id = preg_replace ( '/\D/', '', $this->_getParam ( 'id' ) );
		$menu_block = $model->select ()->setColumns ( array('block'=> array ('class', 'label', 'menu_view' ) ))->getOne ( '`tree`.`id`=' . $menu_id );

		if ($menu_block) {
			$menu = $model->select ()->setColumns ( array ('tree' => array ('id', 'parent_id', 'left','right' ), 'link' => array ('label', 'title', 'class', 'target', 'rel', 'rev', 'resource', 'privilege', 'visible', 'page_id', 'uri', 'ico' ) ) )->setDepth(0)->setParentId ( $menu_id )->getData ();
			if ($menu) {
				$pages_tree = new Pages_Model_Tree ();
				$pages_model = $pages_tree->getModel ( 'page' );
				foreach ( $menu as $key => $value ) {
					if ($value ['page_id'] > 0) {
						$page = $pages_model->select ()->setColumns ( array ('aliasfull' ) )->getOne ( 'id=' . $value ['page_id'] );
						if ($page) {
							$menu [$key] ['uri'] = $page ['aliasfull'];
						}
					}
				}
				$current_url=preg_replace('/\/$/', '',$_SERVER ['REQUEST_URI']);
				$current_url=preg_replace('/\?.*$/', '',$current_url);
				$current_url=$current_url."/";
				$current_url=preg_replace('/^\//', '',$current_url);
				$children = array ();
				$list = array ();
				
				foreach ( $menu as $item ) {
					if (($item ['right'] - $item ['left']) == 1) {
						$item['class'].=" leaf";
					}else{
						$item['class'].=" folder";
					}
					
					$uri=preg_replace('/\/$/', '',$item ['uri']);
					$uri=preg_replace('/^\//', '',$uri);
					$uri=preg_replace('/\?.*$/', '',$uri);
					$uri=$uri."/";
					$uri=preg_replace('/\//', '\\/',$uri);
					
					if(($current_url!="") 
						&&($uri != "") 
						&&(preg_match('/^'.$uri.'/', $current_url))){
						$item['class'].=" active";
					}else{
						
						if(($current_url=="")&&($uri=="\/")){
							$item['class'].=" active";
						}
					}	 
					$list [$item ['id']] = $item;
				}
				foreach ($list as $item) {
					if ((strpos($item['class'], 'active'))){
						$parent_id = $item['parent_id'];
						while (isset($list[$parent_id])) {
							$list[$parent_id]['class'] .= ' active';
							$parent_id = $list[$parent_id]['parent_id'];
						} 
					}
				}

				$flat_list = $list;
				foreach ( $flat_list as $key => $data ) {
					$flat_list [$data ['parent_id']] ['pages'] [$key] = &$flat_list [$key];
				}
				$menu = $flat_list [$menu_id] ['pages'];
				
				$container = new Zend_Navigation ( $menu );
				
				$this->view->menu_id = $menu_id;
				$this->view->class = $menu_block ['class'];
				$this->view->label = $menu_block ['label'];
				$this->view->container = $container;
				$active = $container->findBy ( 'active', true );
				if ($active) {
					Zend_Registry::set ( 'activecont', serialize ( $container ) );
				}
				
				if ($this->_hasParam ( 'menu_view' )) {
					$menu_block ['menu_view'] = $this->_getParam ( 'menu_view' );
				}
				
				
				$this->render($menu_block ['menu_view']);
			}
		}
	}
}